package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.StudentAttendanceDao;
import com.example.domain.StudentAttendance;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.StudentAttendanceService;
import com.example.tool.DateTool;

@Service
public class StudentAttendanceServiceImpl implements StudentAttendanceService {

    @Autowired
    StudentAttendanceDao studentAttendanceDao;

    /**
     * @describe 添加学生考勤信息
     */
    @Override
    public Result addStudentAttendance(StudentAttendance studentAttendance) {
        if (studentAttendance != null) {
            // 获取系统当前时间作为记录创建时间
            studentAttendance.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        StudentAttendance studentAttendance1 = studentAttendanceDao.save(studentAttendance);

        if (studentAttendance1 != null) {
            return ResultFactory.buildSuccessResult(studentAttendance1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新学生考勤信息
     */
    @Override
    public Result updateStudentAttendance(StudentAttendance studentAttendance) {
        if (studentAttendance != null) {
            // 获取系统当前时间作为记录更新时间
            studentAttendance.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        StudentAttendance studentAttendance1 = studentAttendanceDao.saveAndFlush(studentAttendance);

        if (studentAttendance1 != null) {
            return ResultFactory.buildSuccessResult(studentAttendance1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生考勤编号删除学生考勤信息
     */
    @Override
    public Result deleteStudentAttendanceById(String studentAttendanceId) {
        int row = studentAttendanceDao.deleteByStudentAttendanceId(Integer.parseInt(studentAttendanceId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学生考勤信息
     */
    @Override
    public Result queryAllStudentAttendance() {
        List<StudentAttendance> list = studentAttendanceDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询学生考勤信息
     */
    @Override
    public Result queryStudentAttendanceById(String studentAttendanceId) {
        List<StudentAttendance> list = studentAttendanceDao.findByStudentAttendanceId(Integer.parseInt(studentAttendanceId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加学生考勤信息
     */
    @Override
    public Result addStudentAttendanceByExcel(List<StudentAttendance> list) {
        List<StudentAttendance> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = studentAttendanceDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生考勤编号批量删除学生考勤信息
     */
    @Override
    public Result deleteStudentAttendanceByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = studentAttendanceDao.deleteByStudentAttendanceIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 学生考勤信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
//		List<Map<String, Object>> list = null;
        return null;

//		if (list != null) {
//			return ResultFactory.buildSuccessResult(list);
//		} else {
//			return ResultFactory.buildDefaultFailResult();
//		}
    }

}
